<?php
    class StudentDormitoryExchangingBLL{
        public function getStudentList($instructorID,$student_id,$grade,$major,$class,$search,$page,$pageSize){
            $StudentDormitoryExchangingDAL=new StudentDormitoryExchangingDAL();
            $result=$StudentDormitoryExchangingDAL->getStudentList($student_id,$grade,$major,$class,$search,$page*$pageSize,$pageSize);
            $result['page']=$this->getStudentListPage($result['count'],$pageSize);
            return $result;
        }

        //获取学生列表页数
        public function getStudentListPage($count,$pageSize){
            $pageMax=0;
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }
            
            return $pageMax;
        }
        //调换宿舍
        public function exchangeStudentDormitory($exchangeStudent){
            $publicData=new PublicDataBLL();
            $StudentDormitoryExchangingDAL=new StudentDormitoryExchangingDAL();

            $exchange=$publicData->getStudentDormitoryInfo($exchangeStudent->exchange_student);
            $exchanging=$publicData->getStudentDormitoryInfo($exchangeStudent->student);
            $exchangeInfo=array(
                'exchange_student_id'=>$exchangeStudent->exchange_student,
                'exchange_apartment_id'=>$exchange['apartment_id'],
                'exchange_floor'=>$exchange['floor'],
                'exchange_dormitory_id'=>$exchange['dormitory_id'],
                'student_id'=>$exchangeStudent->student,
                'apartment_id'=>$exchanging['apartment_id'],
                'floor'=>$exchange['floor'],
                'dormitory_id'=>$exchanging['dormitory_id'],
            );
            if(!($exchange['apartment_id']==$exchanging['apartment_id'] && $exchange['dormitory_id']==$exchanging['dormitory_id'])){
                $result=$StudentDormitoryExchangingDAL->exchangeStudentDormitory($exchangeInfo);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'调换成功'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'调换失败'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'同一宿舍，无需调换'
                );
            }
            

            return $message;
        }
    }
?>